from rj_casmart import RJ_api
from utils import load_json_to_dict,generate_groups
from classification.match_category import find_first_keyword_match
from rj_casmart.rjmart.pre_api_cx import pre_api_get

# data_list = load_json_to_dict("65_product_detail.json")

# 异常商品 id
product_ids = [200197646386,200197646378,200197646381]

qw = RJ_api()
rj = pre_api_get()
data_list = qw.official_getProductDetailList(product_ids)
print(data_list)

temp_list = []
for product in data_list:
    new_category = find_first_keyword_match(product["name"])
    if new_category:
        print(product["productNum"])
        upload_data = {
            "id": product["id"],
            "code": product["productNum"],
            "name": product["name"],
            "brandId": product["brand"]["id"],
            "categoryId": new_category,
            "photos": product["photos"][:5],
            "unit": product["unit"],
            "deliveryTime": product["deliveryTime"],
            "desc": product["desc"],
            "status": 3,
            "skuInfos": [
                {
                    "code": product["productNum"],
                    "price": product["price"],
                    "directoryPrice": product["price"] * 2,
                    "stock": 1000,
                    "carryFeeTemplateId": product["carryFeeTemplateId"],
                    "deliveryTime": product["deliveryTime"],
                    "unit": product["unit"],
                    "skuAttributes": [
                        {
                            "attributeName": "包装规格",
                            "attributeValue": product["specification"].split(" ")[0],
                        },
                    ],
                }
            ],
        }
        temp_list.append(upload_data)

for group in generate_groups(temp_list):
    res = rj.batchSaveOrUpdateProductV2(group)
    print(res.json())